package com.mycity.services;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.mycity.bean.Comment;
import com.mycity.bean.Post;
import com.mycity.bean.User;
import com.mycity.dao.CommentDao;
import com.mycity.dao.PostDao;
import com.mycity.dao.UserDao;
import com.mycity.util.TagUtil;

public class ProfileService {
	private UserDao userDao=new UserDao();
	private PostDao postDao=new PostDao();
	private CommentDao commentDao=new CommentDao();
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setPostDao(PostDao postDao) {
		this.postDao = postDao;
	}

	public List<Post> getAllPostsByUserId(int userId) throws Exception{
		List<Post> postList;
		try {
			postList = postDao.retrievePostsByUserId(userId);
			setTags(postList);

		} catch (Exception e) {
			throw new Exception(e);
		}
		return postList;
	}

	public int post(int userId, int postType, String postContent, String tags, int locationId, String locationName) throws Exception{
		String today = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date());
		String[] tagList = TagUtil.separate(tags);
		int postId;
		try {
			postId=postDao.writePost(userId, postType, postContent, today, locationId, locationName);
			postDao.setTagsByPostId(tagList, postId);


		} catch (Exception e) {
			postId = -1;
		}
		return postId;
	}

	public List<Post> showRecentPosts() throws Exception{
		List<Post> postList;
		try {
			postList = postDao.recentPosts();
			setTags(postList);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return postList;
	}

	public Post getPostById(int id) throws Exception{
		Post post;
		try {
			post = postDao.getPost(id);
			String[] tagList = postDao.getTagsByPostId(post.getPostId());
			post.setTags(TagUtil.generate(tagList));
		} catch (Exception e) {
			throw new Exception(e);
		}
		return post;
	}

	private void setTags(List<Post> posts){
		for(int i=0; i<posts.size(); i++){
			String[] tagList = postDao.getTagsByPostId(posts.get(i).getPostId());
			posts.get(i).setTags(TagUtil.generate(tagList));
		}

	}

	public List<Comment> getCommentsOfPost(int postId){
		List<Comment> commentList;
		try {
			commentList = commentDao.getCommentsOfPost(postId);
		} catch (Exception e) {
			return null;
		}
		return commentList;
	}

	public void giveStar(int commentId) throws Exception{
		try{
			commentDao.giveStar(commentId);
		} catch (Exception e) {
			throw new Exception(e);
		}

	}

	public int commentOn(String commentContent, int userId, int postId){
		int commentId;
		String today = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date());
		try{
			commentId=commentDao.commentOnPost(commentContent, today, userId, postId);
		} catch (Exception e) {
			return -1;
		}
		return commentId;
	}

	public boolean deleteComment(int commentId){
		try{
			commentDao.removeStar(commentId);
			commentDao.deleteComment(commentId);
			return true;
		}
		catch(Exception e){
			return false;
		}
	}
	
	public boolean removeStar(int commentId){
		try{
			commentDao.removeStar(commentId);
			return true;
		}
		catch(Exception e){
			return false;
		}
	}

	public boolean removeDiamond(int postId, int userId){
		try{
			postDao.removeDiamond(postId, userId);
			return true;
		}
		catch(Exception e){
			return false;
		}
	}
	
	public boolean deletePost(int postId){
		try{
		List<User> diamonders = postDao.whoDiamonded(postId);
		for(int i=0; i<diamonders.size(); i++)
			postDao.removeDiamond(postId, diamonders.get(i).getUserId());
		List<Comment> comments = commentDao.getCommentsOfPost(postId);
		for(int i=0; i<comments.size(); i++)
			deleteComment(comments.get(i).getCommentId());
		postDao.deletePost(postId);
		return true;
		}
		catch(Exception e){
			return false;
		}
	}

	public void giveDiamond(int postId, int userId){
		String today = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date());
		postDao.giveDiamond(postId, userId, today);
	}

	public User getUserById(int userId){
		User user = userDao.getUserById(userId);
		return user;
	}

	public int updateBio(int userId, String bio){
		try{
			userDao.setBio(userId, bio);
			return 1;
		}
		catch(Exception e){
			return -1;
		}
	}

	public int changeProfilePic(int userId, String profilePic){
		try{
			userDao.setProfilePic(userId, profilePic);
			return 1;
		}
		catch(Exception e){
			return -1;

		}

	}

	public List<Post> diamondedPosts(int userId) throws Exception{
		List<Post> postList;
		try {
			postList = postDao.getPreviousDiamondedPosts(userId);
			setTags(postList);
		} catch (Exception e) {
			return null;
		}
		return postList;
	}

	public boolean isPostDiamonded(int userId, int postId) throws Exception{
		List<Post> postList;
		try {
			postList = postDao.diamondedPost(userId, postId);
			setTags(postList);
		} catch (Exception e) {
			return false;
		}
		return (postList.size()==1);
	}
}
